package com.hm.pan.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.hm.pan.annotation.CheckLogin;
import com.hm.pan.annotation.NoLogin;
import com.hm.pan.model.ResultObj;
import com.hm.pan.model.Share;
import com.hm.pan.model.UserFile;
import com.hm.pan.service.ShareService;
import com.hm.pan.service.UserFileService;
import com.hm.pan.util.Generator;

@RestController
@CheckLogin
public class ShareController {

	@Resource
	private ShareService shareService;

	@Resource
	private UserFileService userFileService;

	@PostMapping("judgeShareCode")
	public Object findById(@RequestParam("sharePassword") String sharePassword,
			@RequestParam("shareId") Integer shareId) {
		ResultObj resultObj = new ResultObj();
		System.out.println(sharePassword);
		try {
			if (sharePassword == null) {
				resultObj.setMessage("success");
			} else if (sharePassword.equals(shareService.findById(shareId).get("share_password"))) { // 判断输入的提取码与数据库的是否正确
				resultObj.setMessage("success");
			} else {
				resultObj.setMessage("fails");
			}
		} catch (NullPointerException e) {
			resultObj.setMessage("success");
			// TODO: handle exception
		}
		return resultObj;
	}

	// 通过id参数查找用户名和判断有没有提取码，没有提取码直接进入界面
	@PostMapping("shareById")
	@NoLogin
	public Object findUserName(@RequestParam("shareId") Integer shareId) {
		ResultObj resultObj = new ResultObj();
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("name", shareService.findUserName(shareId).get("user_name"));
			map.put("photo", shareService.findUserName(shareId).get("user_photo"));
			map.put("sharePassword", shareService.findUserName(shareId).get("share_password"));
			resultObj.setData(map);
			resultObj.setMessage("success");
			resultObj.setStatusCode(200);
		} catch (NullPointerException e) {
			// TODO: handle exception
		}

		return resultObj;
	}

	// 生成share链接和提取码
	@PostMapping("addShare")
	public JSONObject addShare(@RequestBody JSONObject jsonObject, Share share) {
		Timestamp shareTime = new Timestamp(System.currentTimeMillis()); // 获取生成时间
		String extractCode = jsonObject.getString("extractCode"); // 是否生成提取码，is则是，not则不生成
		String sharePassword = null;
		if (extractCode.equals("is")) {
			sharePassword = Generator.extractCode(); // 生成提取码
		} else {
			sharePassword = null;
		}
		share.setUserfileId(jsonObject.getInteger("userFileId")); // 获取文件id
		share.setShareDay(jsonObject.getInteger("shareDay")); // 获取过期时间
		share.setShareTime(shareTime);
		share.setUserId(jsonObject.getInteger("userId")); // 获取用户ID
		share.setSharePassword(sharePassword); // 获取提取码
		shareService.addShare(share); // 向数据库添加一条分享信息
		Map<String, Object> map = new HashMap<>();
		map.put("shareId", share.getShareId()); // 将分享的id号装入shareId
		map.put("sharePassword", sharePassword); // 将提取码装入sharePassword
		JSONObject fluentPutAll = jsonObject.fluentPutAll(map); // 使用jsonObject.fluentPutAll()装数据
		return fluentPutAll; // 将数据发送至前端处理
	}

	// 通过分享id查询分享信息
	@PostMapping("findShareId")
	@NoLogin
	public Object findShareId(@RequestParam("shareId") Integer shareId) throws NullPointerException {
		ResultObj resultObj = new ResultObj();
		Integer userFileId = shareService.findShareId(shareId);
		if (userFileId != null) {
			resultObj.setMessage("success");
			resultObj.setData(userFileId);
			resultObj.setStatusCode(200);
		} else {
			resultObj.setMessage("fails");
			resultObj.setStatusCode(500);
		}
		return resultObj;
	}

	// 通过获取文件ID获取文件信息
	@PostMapping("findUserFile")
	@NoLogin
	public Object findUserFile(@RequestParam("userfileId") Long userfileId, @RequestParam("shareId") Long shareId) {
		ResultObj resultObj = new ResultObj();
		UserFile filter = new UserFile();
		filter.setUserfileId(userfileId);
		try {
			UserFile oneFile = userFileService.getOneFile(filter);
			Object oneShare = shareService.getOneShare(userfileId, shareId);
			Map<String, Object> map = new HashMap<>();
			map.put("file", oneFile);
			map.put("share", oneShare);
			resultObj.setMessage("success");
			resultObj.setStatusCode(200);
			resultObj.setData(map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultObj.setMessage("fails");
			resultObj.setStatusCode(200);
		}
		return resultObj;
	}
}
